package com.yewen.interceptors;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Stack;

/**
 * @Classname LoginIntercepter
 * @Description 验证登陆、否则跳到登陆界面
 * @Date 2022/4/4 22:53
 * @Created by TaoVh
 */
public class LoginInterceptor implements HandlerInterceptor {
    private static Logger logger = LoggerFactory.getLogger(LoginInterceptor.class);
    private static String[] NOT_INTERCEPTOR =  {"/yewen/pages/login",
            "",
            "/yewen/pages/register",
            "/yewen/account/educounselorRegister",
            "/yewen/account/studentRegister",
            "/yewen/account/loginAuth",
            "/yewen/ranking/contributeUniversity",
            "/yewen/ranking/getUniversityRanking",
            "/yewen/ranking/getMajorRanking",
            "/yewen/ranking/contributeMajor"};
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        for(String s:NOT_INTERCEPTOR){
            if (request.getRequestURI().equals(s))return true;
        }
        if (request.getSession()==null){
            logger.error("[LoginInterceptor][session为null]");
            response.sendRedirect("http://120.25.230.224:8080/notLogin");
            return false;
        }
        HttpSession session = request.getSession();
        if (session.getAttribute("isLogin")==null){
            response.sendRedirect("http://120.25.230.224:8080/notLogin");
            return false;
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
